import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from "element-ui";
import 'element-ui/lib/theme-chalk/index.css';
import store from './store'
import {getRequest, postRequest,deleteRequest,putRequest} from './utils/api'
import {initMenu} from './utils/menus'
import 'font-awesome/css/font-awesome.css'

Vue.use(ElementUI,{size:'small'});
// 运行环境
Vue.config.productionTip = false
Vue.prototype.getRequest = getRequest
Vue.prototype.postRequest = postRequest
Vue.prototype.deleteRequest = deleteRequest
Vue.prototype.putRequest = putRequest


router.beforeEach((to, from, next) => {
    if (window.sessionStorage.getItem('tokenStr')) {
        initMenu(router, store);
        //判断用户信息是否存在
        if (!window.sessionStorage.getItem('user')) {
            return postRequest('/admin/info').then(resp => {
                if (resp) {
                    //存入用户信息
                    window.sessionStorage.setItem('user', JSON.stringify(resp));
                    next();
                }
            })
        }
        next();
    } else {
        if (to.path =='/'){
            next();
        }else {
            next('/redirect='+ to.path)
        }

    }
})


new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
